<?php

namespace Beerus\Modules\Wiki\Controllers;

use App\Admin\Controllers\WorkBaseController;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Biz\tools\ImgSearch;

class AliTestController extends WorkBaseController
{
    public function index(Request $request, Content $content)
    {
        Admin::style('.content-wrapper {margin-left:0px;}');
        Admin::style('.main-sidebar {display:none;}');
        Admin::style('.main-header {display:none;}');
        $list = [] ;
        $picurl = $request->input('picurl', '');
        if($picurl){
           $result =  ImgSearch::searchByUrl($picurl);
            if($result['body']['Data']['Auctions'] && is_array($result['body']['Data']['Auctions'])){
                 foreach ($result['body']['Data']['Auctions'] as $val){
                     $list[] = [
                         'ZkFinalPrice' => $val['Result']['ZkFinalPrice'],
                         'Title' => $val['Result']['Title'],
                         'PicUrl' => "http:".$val['Result']['PicUrl'],
                         'Url' => "http:".$val['Result']['Url'],
                         'score'=>  $val['RankScore']
                     ];
                 }
            }
        }

        $gridModel = new pGridDataModel($list, 10, []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disablePagination();
            $grid->disableExport();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->dropDown('url', [
                        'picurl' => '图片地址',
                    ]);
                $filter->expand();
            });
            $grid->column('Title', '商品描述');
            $grid->column('score', '相似度');

            $grid->column('ZkFinalPrice', '价格');
            $grid->column('PicUrl', '商品图片')->display(function() {
                return "<img src='".$this->PicUrl."' width=300 />";
            })->width(350);
            $grid->column('Url', '淘宝链接')->display(function() {
                if($this->Url) {
                    return "<a href='".$this->Url."' target='_blank'>点击跳转</a>";
                } else {
                    return '-';
                }
            })->width(150);
            $grid->disableActions();
        });

        return $content
            ->header('以图搜商品')
            ->body($listGrid);
    }




}